// ImageLoader.cpp
//
#include "ImageLoader.h"
#include "Image.h"

#include <fstream>
#include <QImage>
#include <QGLWidget>

//#include "Utils/MemCheck.h"

using namespace OpenGL;

bool ImageLoader::load( const std::string &filename, Image &img )
{
	bool loaded = false;


#if 1 // load using QT

	std::ifstream is(filename);
	if( !is.is_open() )
		return loaded;
	is.close();

	QImage qImage(filename.c_str());

	if( !qImage.isNull() )
	{
		qImage    = QGLWidget::convertToGLFormat(qImage);
		int w	  = qImage.width();
		int h	  = qImage.height();
		int numCh = qImage.hasAlphaChannel() ?  4 : 3;

		unsigned char *pixels = new unsigned char [w*h*numCh];
		memcpy(pixels,qImage.bits(),sizeof(unsigned char)*w*h*numCh);

		img.create( w,h, numCh, pixels );
		loaded = true;
	}

#endif
	
	
	return loaded;
}
